package org.bud.rung.config;

import com.alibaba.fastjson.JSON;
import org.assertj.core.util.Lists;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;

/**
 * @author shaun
 * @date 2024/8/11
 * @instruction
 */
public class TestWn {
    private final Map<String, Map<String,Object>> lockMap = new ConcurrentHashMap<>();
    private static volatile Map<String, Map<String,Object>> data = new ConcurrentHashMap<>();
    private Object lock = new Object();
    public void test(){
        new Thread(() -> {
            Lists.newArrayList("hello","hell")
                    .stream()
                    .forEach(o -> {
                        final Map<String,Object> map = new HashMap<>();
                        map.put("read",new Object());
                        map.put("write",new Object());
                        lockMap.put(o,map);
                    });
            synchronized (lock){
                lock.notify();
            }

        }).start();
        new Thread(() -> {
            synchronized (lock) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println(111111);

                Lists.newArrayList("hello","hell")
                        .stream()
                        .forEach(o -> {
                            new Thread(() -> {
                            Map<String, Object> map = new HashMap<>();
                            map.put("11", o + "111");
                            map.put("22", o + "222");
                            data.put(o, map);
                            synchronized (lockMap.get(o).get("read")) {
                                lockMap.get(o).get("read").notify();
                            }
                                System.out.println(111111);
                            }).start();
                        });

                Lists.newArrayList("hello","hell")
                        .stream()
                        .forEach(o -> {
                            new Thread(() -> {
                                synchronized (lockMap.get(o).get("read")) {
                                    try {
                                        lockMap.get(o).get("read").wait(10);
                                    } catch (InterruptedException e) {
                                        throw new RuntimeException(e);
                                    }
                                }
                                System.out.println(JSON.toJSONString(data.get(o)));
                            }).start();
                        });


        }).start();
    }

    public static void main(String[] args) {
        new TestWn().test();
    }

}
